package com.itheima.controller;

import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogSortService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.BlogSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客分类相关接口
 */
@RestController
@RequestMapping("/blogSort")
@Api(tags = "博客分类相关接口")
@CrossOrigin
public class BlogSortController {
    @Autowired
    BlogSortService blogSortService;

    //添加博客分类
    @PostMapping("/add")
    @ApiOperation("添加博客分类")
    public String add(@RequestBody BlogSortVO blogSortVO){
        try {
            blogSortService.add(blogSortVO);
            return ResultUtil.result("success",MessageConstant.BLOGSORT_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_ADD_FAIL);
        }
    }

    //通过引用量排序博客分类
    @PostMapping("/blogSortByCite")
    @ApiOperation("通过引用量排序博客分类")
    public String blogSortByCite(){
        try {
            List<BlogSort> blogSortList = blogSortService.blogSortByCite();
            return ResultUtil.result("success",blogSortList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_BY_CITE_FAIL);
        }
    }

    //通过点击量排序博客分类
    @PostMapping("/blogSortByClickCount")
    @ApiOperation("通过点击量排序博客分类")
    public String blogSortByClickCount(){
        try {
            List<BlogSort> blogSortList=blogSortService.blogSortByClickCount();
            return ResultUtil.result("success",blogSortList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_BY_CLICK_COUNT_FAIL);
        }
    }

    //批量删除博客分类
    @PostMapping("/deleteBatch")
    @ApiOperation("批量删除博客分类")
    public String deleteBatch(@RequestBody BlogSortVO blogSortVO){
        if (blogSortVO.getUid()==null||"".equals(blogSortVO.getUid())){
            return ResultUtil.result("error","请选择要删除的博客分类");
        }
        try {
            blogSortService.deleteBatch(blogSortVO);
            return ResultUtil.result("success",MessageConstant.BLOGSORT_DELETE_BATCH_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_DELETE_BATCH_FAIL);
        }
    }

    //编辑博客分类
    @PostMapping("/edit")
    @ApiOperation("编辑博客分类")
    public String edit(@RequestBody BlogSortVO blogSortVO){
        if (blogSortVO.getUid()==null||"".equals(blogSortVO.getUid())){
            return ResultUtil.result("error","请选择要编辑的博客分类");
        }
        try {
            blogSortService.edit(blogSortVO);
            return ResultUtil.result("success",MessageConstant.BLOGSORT_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_EDIT_FAIL);
        }
    }

    //获取博客分类列表
    @PostMapping("/getList")
    @ApiOperation("获取博客分类列表")
    public String edigetListt(@RequestBody BlogSortVO blogSortVO){
        try {
            List<BlogSort> blogSortList= blogSortService.getList(blogSortVO);
            return ResultUtil.result("success",blogSortList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_FINDALL_FAIL);
        }
    }

    //置顶分类
    @PostMapping("/stick")
    @ApiOperation("置顶分类")
    public String stick(@RequestBody BlogSortVO blogSortVO){
        if (blogSortVO.getUid()==null||"".equals(blogSortVO.getUid())){
            return ResultUtil.result("error","请选择要置顶的博客分类");
        }
        try {
            List<BlogSort> blogSortList=blogSortService.stick(blogSortVO);
            return ResultUtil.result("success",blogSortList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOGSORT_DELETE_BATCH_FAIL);
        }
    }

}
